package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVHomeWorkList;
import com.beetlestech.nfe.main.biz.com.dto.VhomeworkinfolistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.ExaminationdocDTO;
import com.beetlestech.nfe.main.db.tbl.MsthomeworkDTO;
import com.beetlestech.nfe.main.db.view.VhomeworkinfolistDTO;

public class HbVHomeWorkListImpl extends AbstractDBLogic implements HbVHomeWorkList, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	@Override
	public VhomeworkinfolistInfoDTO[] getHomeWorkList(String homeWorkname,
			Integer typeCid) {
		List<VhomeworkinfolistInfoDTO> homeWorkList = new ArrayList<VhomeworkinfolistInfoDTO>();
		List<VhomeworkinfolistDTO> homeWorkListDB = new ArrayList<VhomeworkinfolistDTO>();
		VhomeworkinfolistDTO vhomeworkinfolistDTO =new VhomeworkinfolistDTO();
		StatementDTO statementDTO = new StatementDTO();
		if(StringUtil.isNotEmpty(homeWorkname)){
			StatementUtil.andWhere(statementDTO, VhomeworkinfolistDTO.COLUMN_HOMEWORK_NAME, StatementUtil.KEYWORD.LIKE ,homeWorkname);
		}
		if(typeCid!=0){
			StatementUtil.andWhere(statementDTO, VhomeworkinfolistDTO.COLUMN_CATAGORY_C_ID, StatementUtil.KEYWORD.EQUAL ,typeCid);
//			StatementUtil.andWhere(statementDTO, VhomeworkinfolistDTO.COLUMN_CATAGORY_P_ID, StatementUtil.KEYWORD.EQUAL ,CATAGORY_P_COURESE_TYPE.OWNER);
		}
		vhomeworkinfolistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		homeWorkListDB=queryDAO.queryForObjectList(VhomeworkinfolistDTO.SQL_SEARCH, vhomeworkinfolistDTO);
		if(homeWorkListDB==null || homeWorkListDB.size()==0){
			createApplicationException("errMsg", "没找到要查询的信息");
		}else{
			for(VhomeworkinfolistDTO item:homeWorkListDB){
				homeWorkList.add(convertToVhomeWorkListInfo(item));
			}
		}
		return homeWorkList.toArray(new VhomeworkinfolistInfoDTO[homeWorkList.size()]);
		
	}
	private VhomeworkinfolistInfoDTO convertToVhomeWorkListInfo(VhomeworkinfolistDTO item){
		VhomeworkinfolistInfoDTO vhomeworkinfolistInfoDTO = new VhomeworkinfolistInfoDTO();
		vhomeworkinfolistInfoDTO.sethomeworkid(item.gethomeworkid());
		vhomeworkinfolistInfoDTO.setcatagorycname(item.getcatagorycname());
		vhomeworkinfolistInfoDTO.sethomeworkname(item.gethomeworkname());
		vhomeworkinfolistInfoDTO.setsenddate(item.getsenddate());
		vhomeworkinfolistInfoDTO.setsendtime(item.getsendtime());
		vhomeworkinfolistInfoDTO.setsendername(item.getsendername());
		return vhomeworkinfolistInfoDTO;
	}
	@Override
	public void deleteHomeWork(String actionType, String homeWorkSql,
			Integer homeWorkId) {


		if(actionType.equals("single")){
			MsthomeworkDTO homeWorkDTO =new MsthomeworkDTO();
			if(homeWorkId==null){
				return ;
			}
			homeWorkDTO.sethomeworkid(homeWorkId);;
			updateDAO.updateOne(MsthomeworkDTO.DELETE, homeWorkDTO, M10190WS);
		}else if(actionType.equals("many")){
			String[] dbSqlArray=homeWorkSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				if(StringUtil.isEmpty(dbSqlArray[i])){
					break;
				}
				MsthomeworkDTO homeWorkDTO =new MsthomeworkDTO();
				homeWorkDTO.sethomeworkid(Integer.parseInt(dbSqlArray[i]));;
				updateDAO.updateOne(MsthomeworkDTO.DELETE, homeWorkDTO, M10190WS);
			}
		}
	
	
		
	}

	
}
